/*datagrid行编辑扩展。editorOptions参数示例：
editorOptions:{
	sqlId:{
		insert:"xx",
		update:"xx",
		delete:["xx","xx"]
	},
	dbName:"db1",
	validateOnBlur:true,
	validate:function(tab,idx){if (xx) return true; else return false;},
	onSelect:function(tab,rowIndex,rowData){},
	onBeforeSave:function(tab){}, //返回false时退出saveChanges方法
	onAfterSave:function(tab){},
	onSaveSuccess:function(tab){},
	onSaveError:function(tab){},
	showSaveMessage:true, //默认值:true
	saveSuccessMessage:"xx", //默认值:"保存成功！"
	saveErrorMessage:"xx" //默认值:"保存失败！"
}*/
(function($){
	$.fn.datagrid.defaults.onBeforeLoad = function(param){
		if ($.data(this,"datagrid").options.editorOptions) $(this).datagrid("rejectChanges").data("lastEditRowIndex",-1);
	};
	$.fn.datagrid.defaults.onSelect = function(rowIndex,rowData){
		var eo = $.data(this,"datagrid").options.editorOptions;
		if (!eo) return;
		var tab = $(this), idx = tab.data("lastEditRowIndex");
		if (rowIndex == idx) return; // 就是选中行
		if ((eo.validateOnBlur==undefined || eo.validateOnBlur==true) && !validateRow(tab,idx)) { // 上一个编辑行信息填写校验不通过
			tab.datagrid("selectRow", idx);
			setTimeout(function(){tab.datagrid("unselectRow", rowIndex);},1);
			return;
		}
		tab.datagrid("endEdit",idx).datagrid("beginEdit",rowIndex).data("lastEditRowIndex",rowIndex);
		var s = $.data(this,"datagrid").options.editorOptions.onSelect; // 选中事件接口
		if (s) s(tab,rowIndex,rowData);
	};
	$.fn.datagrid.methods.addRow = function(jq, row){
		return jq.each(function(){
			var tab = $(this), idx = tab.data("lastEditRowIndex"), eo = $.data(this,"datagrid").options.editorOptions;;
			if ((eo.validateOnBlur==undefined || eo.validateOnBlur==true) && !validateRow(tab,idx)) return;
			tab.datagrid("endEdit",idx).data("lastEditRowIndex",-1).datagrid("appendRow",row||{}).datagrid("selectRow",tab.datagrid("getRows").length-1);
		});
	};
	$.fn.datagrid.methods.removeRow = function(jq){
		return jq.each(function(){
			var tab = $(this), idx = tab.data("lastEditRowIndex");
			if (idx==-1) return;
			tab.datagrid("deleteRow", idx).data("lastEditRowIndex", -1);
		});
	};
	$.fn.datagrid.methods.saveChanges = function(jq, p){
		return jq.each(function(){
			var tab = $(this), idx = tab.data("lastEditRowIndex"), op = $.data(this,"datagrid").options.editorOptions, sid = op.sqlId;
			if ((op.validateOnBlur==undefined || op.validateOnBlur==true) && !validateRow(tab,idx)) return;
			tab.datagrid("endEdit",idx).data("lastEditRowIndex",-1);
			if (tab.datagrid("getChanges").length==0 && !p) return; // 无变更且无额外更新内容

			if (op.onBeforeSave) { // 保存前事件接口
				var f = op.onBeforeSave(tab);
				if (f!=undefined && f==false) return; // 返回false时退出
			}
			var ids = [], params = [];
			var del = tab.datagrid("getChanges", "deleted");
			if (del.length>0) addParam(sid["delete"], del);
			var ins = tab.datagrid("getChanges", "inserted");
			if (ins.length>0) addParam(sid.insert, ins);
			var upd = tab.datagrid("getChanges", "updated");
			if (upd.length>0) addParam(sid.update, upd);
			if (p && p.sqlId) { // 除了表格更新外的额外执行语句
				ids.push(p.sqlId);
				params.push(p.sqlParam);
			}
			$.post("common/modify",{sqlId:JSON.stringify(ids),sqlParam:JSON.stringify(params),dbName:op.dbName||""},function(data){
				if (op.onAfterSave) op.onAfterSave(tab);  // 保存后事件接口
				if (data=="success"){
					tab.datagrid("acceptChanges").data("lastEditRowIndex",-1).datagrid("load");
					if (op.onSaveSuccess) op.onSaveSuccess(tab);  // 保存后事件接口
					if (op.showSaveMessage!=false) $.messager.alert("提示", op.saveSuccessMessage||"保存成功！", "info");
				} else {
					if (op.onSaveError) op.onSaveError(tab);  // 保存后事件接口
					if (op.showSaveMessage!=false) $.messager.alert("提示", op.saveErrorMessage||"保存失败！", "error");
				}
			});
			function addParam(sqlId,sqlParam) {
				if (sqlId instanceof Array) {
					for (var i=0; i<sqlId.length; i++) {
						ids.push(sqlId[i]);
						params.push(sqlParam);
					}
				} else {
					ids.push(sqlId);
					params.push(sqlParam);
				}
			}
		});
	};
	$.fn.datagrid.methods.validate = function(tab){
		return validateRow(tab, tab.data("lastEditRowIndex"));
	};
	$.fn.datagrid.methods.getSelectedRowIndex = function(tab) {
		return tab.data("lastEditRowIndex");
	};
	function validateRow(tab, idx){
		if (idx==-1) return true;
		var ret = true, v = $.data(tab[0],"datagrid").options.editorOptions.validate; // 校验接口
		if (v) ret = v(tab, idx);
		return ret && tab.datagrid("validateRow", idx);
	}
})(jQuery);
